package com.stone.bigdata.controller;

import com.stone.bigdata.domain.AddCount;
import com.stone.bigdata.domain.VO.CityTotalVO;
import com.stone.bigdata.domain.VO.LeftPageVO;
import com.stone.bigdata.domain.VO.TodayAddVO;
import com.stone.bigdata.service.ActiveCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class ActiveCityController {
    @Autowired
    ActiveCityService activeCityService;

//    @RequestMapping(value = "/api/saveActiveCity", method = RequestMethod.GET)
//    public void saveActiveCity() {
//        activeCityService.saveActiveCity();
//    }

    @RequestMapping(value = "/api/getSixDayAdd", method = RequestMethod.GET)
    public List<AddCount> getSixDayAdd() {
        LocalDateTime now = LocalDateTime.now();
        List<AddCount> list = activeCityService.listSixDayAdd(Date.valueOf(now.minusDays(6).toLocalDate()), Date.valueOf(now.minusDays(1).toLocalDate()));
        return list;
    }

    @RequestMapping(value = "/api/poc/listActiveCity", method = RequestMethod.GET)
    public List<CityTotalVO> listActiveCity() {
        LocalDateTime now = LocalDateTime.now();
        List<CityTotalVO> list = activeCityService.listActiveCity(Date.valueOf(now.minusDays(1).toLocalDate()));
        return list;
    }

    @RequestMapping(value = "/api/getFiveAfter", method = RequestMethod.GET)
    public LeftPageVO getFiveAfter() {
        LocalDateTime now = LocalDateTime.now();
        Timestamp todayZero = Timestamp.valueOf(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
        Timestamp fiveStamp = Timestamp.valueOf(LocalDateTime.of(now.toLocalDate(), LocalTime.of(now.getHour(), now.getMinute(), 0)));
        List<AddCount> list= activeCityService.listFiveAfter(todayZero, fiveStamp);
        if (list != null) {
            List<Integer> result = new ArrayList<>();
            Timestamp startStamp = list.get(0).getCreateTime();
            if (!startStamp.equals(todayZero)) {
                LocalDateTime startLocal = startStamp.toLocalDateTime();
                System.out.println(startLocal);
                int initial = (startLocal.getHour() * 60 + startLocal.getMinute()) / 5;
                System.out.println(initial);
                for (int j = 0; j < initial; j++) {
                    result.add(0);
                }
            }
            for (int i = 0; i < list.size(); i ++) {
                result.add(list.get(i).getTotal());
            }
            LeftPageVO leftPageVO = new LeftPageVO();
            TodayAddVO todayAddVO = new TodayAddVO();
            Integer total = list.get(list.size() - 1).getTotal();
            Integer average = total / (now.getHour() + 1);
            todayAddVO.setTodayTotalAdd(total);
            todayAddVO.setTodayAverageAdd(average);
            leftPageVO.setTodayAddVO(todayAddVO);
            leftPageVO.setList(result);
            return leftPageVO;
        } else {
            return new LeftPageVO();
        }
    }
}
